#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cerrno>
#include <cstring>
#include <cstdio>
using namespace std;

static bool SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if(fl < 0)
    {
        cerr << "fcntl err" << endl;
    }
    fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}
int main()
{
    SetNonBlock(0);
    while(1)
    {
        char buffer[1024];
        ssize_t n = read(0, buffer, sizeof(buffer) - 1);
        if(n > 0)
        {
            buffer[n - 1] = 0;
            cout << "echo# " << buffer << endl;
        }
        else if(n == 0)
        {
            cout << "read over" << endl;
            break;
        }
        else
        {
            if(errno == EWOULDBLOCK)
            {
                cout << "time out..." << endl;
                sleep(1);
            }
            else
            {
                printf("read err, errno: %d, err string: %s\n", errno, strerror(errno));
                break;
            }
            
        }
    }
    return 0;
}